package control;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.service.MovieService;
import model.service.MyCinemaService;
import model.vo.mycinema.BoardVO;
import model.vo.mycinema.ListVOGenericMap;
import model.vo.mycinema.MemberLevelInfoVO;
import model.vo.mycinema.MemberVO;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

public class MyCinemaController extends MultiActionController {
		private MyCinemaService myCinemaService;
		private MovieService movieService;
		
	public MyCinemaController() {
			super();
		}
	public MyCinemaController(MyCinemaService myCinemaService){
		this.myCinemaService=myCinemaService;
	}	
	public void setMovieService(MovieService movieService){
		this.movieService = movieService;
	} 
	/**
	 * layout1 (초기화면) - member
	 */
	public ModelAndView home_main(HttpServletRequest request, HttpServletResponse response){
		System.out.println("home_main join..");
		ArrayList<HashMap> list=null;
		try {
			list = myCinemaService.searchMovie("어벤져스");
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return new ModelAndView("home_main","list",list);
	}
	/**
	 * 마이씨네마 - 예매/취소 내역 (초기화면)
	 */
	public ModelAndView getMyHistory(HttpServletRequest request, HttpServletResponse response){
		System.out.println("getMyHistory join..");
		ListVOGenericMap lvo = null;
		if (request.getSession().getAttribute("vo") != null) {
			MemberVO vo = (MemberVO) request.getSession().getAttribute("vo");
			Map map = new HashMap();
			map.put("member_no", vo.getMember_no());
			String page = request.getParameter("page");
			if (page == null || page == "")
				page = "1";
			map.put("page", page);
			String itemList = request.getParameter("itemList");
			if (itemList == null || itemList == "")
				itemList = "all";
			if (itemList.equals("all")) {
				lvo = myCinemaService.getAllMyHistory(map);
			} else if (itemList.equals("buy")) {
				lvo = myCinemaService.getBuyMyHistory(map);
			} else if (itemList.equals("sell")) {
				lvo = myCinemaService.getSellMyHistory(map);
			}
			if (request.getParameter("ajax") != null) {
				return new ModelAndView("JsonView", "list", lvo);
			}
		}
		return new ModelAndView("my_history.mycinema", "list", lvo);
	}
	/**
	 * 개인정보 방침으로 이동
	 */
	public ModelAndView contract(HttpServletRequest request, HttpServletResponse response) throws Exception{
		System.out.println("개인 정보로 이동");
		return new ModelAndView("contract.member");
	}
	/**
	 * 마이씨네마 - 내 정보 수정
	 */
	public ModelAndView getMyInfo(HttpServletRequest request, HttpServletResponse response){
		System.out.println("getMyInfo join..");
		return new ModelAndView("my_info.mycinema");
	}
	/**
	 * 마이씨네마 - 고객센터(리스트 보기)
	 * @throws Exception 
	 */
	public ModelAndView board(HttpServletRequest request, HttpServletResponse response) throws Exception{
		System.out.println("board join..");
		return new ModelAndView("cs_list.mycinema","lvo",myCinemaService.getBoardList(request.getParameter("pageNo")));
	}
	/**
	 * 마이씨네마 - 회원탈퇴로 이동하기
	 */
	public ModelAndView leave(HttpServletRequest request, 
			HttpServletResponse response) throws Exception{
		System.out.println("leave join..");
		return new ModelAndView("leave.mycinema");
	}
	/**
	 * 회원 탈퇴하기
	 */
	public ModelAndView deleteMember(HttpServletRequest request, 
			HttpServletResponse response, String member_no) throws Exception{
		System.out.println("deleteMember ..");		
		HttpSession session=request.getSession();
		MemberVO vo=(MemberVO)session.getAttribute("vo");			
				System.out.println(vo+"findId");
		String name=request.getParameter("name");
		String pass=request.getParameter("password");
		System.out.println(name+" "+pass+"확인");
		if(vo.getName().equals(name) && vo.getPassword().equals(pass)){
			int no=vo.getMember_no();
			System.out.println(no+" "+"삭제");
			System.out.println(no+" "+"삭제할 회원번호");
					myCinemaService.deleteMember(vo, no);
					if(session!=null)
						session.invalidate();	
			return new ModelAndView("delete_result.mycinema");
		}else{			
			System.out.println("조건일치 하지 않음");
			request.setAttribute("vo", vo);
		}											
		return new ModelAndView("leave.mycinema");
	}
	/**
	 * 회원 가입으로 이동
	 */
	public ModelAndView move(HttpServletRequest request, HttpServletResponse response) throws Exception{
		System.out.println("회원등록으로 이동");
		return new ModelAndView("register.member");
	}
	/**
	 * 회원가입
	 */
	public ModelAndView register(HttpServletRequest request, HttpServletResponse response) throws Exception{
		MemberVO vo=new MemberVO();
		String path="";
		this.bind(request, vo);	
		MemberLevelInfoVO memberlevelinfoVO=new MemberLevelInfoVO();
		vo.setMemberlevelinfoVO(memberlevelinfoVO);
		vo.getMemberlevelinfoVO().setMember_level_no(0);
		vo.setMemberlevelinfoVO(memberlevelinfoVO);
		vo.getMemberlevelinfoVO().setMember_level("회원");
		System.out.println(vo);				
		try {
			myCinemaService.registerMember(vo);
			if(vo!=null){
				request.getSession().setAttribute("vo", vo);
				 path="mycinema.do?command=home_main";
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return new ModelAndView("home_main","vo",vo);
	}
	/**
	 * 회원 가입시 아이디 체크
	 * 
	 */
	public ModelAndView idcheck(HttpServletRequest request,
			HttpServletResponse response) throws Exception{	
		boolean idflag=false;
		try {
			idflag=myCinemaService.idcheck(request.getParameter("id"));		
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return new ModelAndView("JsonView","idflag",idflag);
	}
	/**
	 * 로그인 ajax
	 * 로그인 성공시
	 * 	관리자면 key admin
	 * 	회원이면 key member
	 * 로그인 실패시
	 * 	key fail 
	 */
	public ModelAndView login(HttpServletRequest request, 	HttpServletResponse response,MemberVO mvo) throws Exception{
		System.out.println("login join.. vo : "+mvo);
		HttpSession session = request.getSession();
		MemberVO rvo=myCinemaService.login(mvo);
		HashMap map = new HashMap();
		if(rvo!=null){
			session.setAttribute("vo", rvo);
			map.put("vo", rvo);
		}else{
			map.put("fail","N");
		}
		return new ModelAndView("JsonView","map",map);
	}
	/**
	 * 로그아웃
	 *
	 */
	public ModelAndView logout(HttpServletRequest request,HttpServletResponse response,HttpSession session) throws Exception{
		if(session!=null)
			session.invalidate();
		return new ModelAndView("home_main");
	}
	/**
	 * 글쓰기 창 이동
	 *
	 */
	public ModelAndView writeMove(HttpServletRequest request,HttpServletResponse response,HttpSession session) throws Exception{
		System.out.println("writeMove join...");
		return new ModelAndView("write.mycinema");
	}
	/**
	 * 게시판에 글쓰기
	 * 
	 */
	public ModelAndView write(HttpServletRequest request,HttpServletResponse response,
			HttpSession session,BoardVO bvo) throws Exception{	
		MemberVO vo=(MemberVO)session.getAttribute("vo");
			System.out.println("글쓰기 1번");
			String isPublic = "0";
			
			bvo.setIsPublic(isPublic);
			bvo.getIsPublic();
			bvo.setMemberVO(vo);
			System.out.println(bvo+" "+"확인");
			myCinemaService.write(bvo);
			
		return new ModelAndView("show.mycinema","bvo",bvo);
	}
	/**
	 * 작성한 글 보기
	 */
	public ModelAndView show(HttpServletRequest request,HttpServletResponse response,
			HttpSession session,BoardVO bvo) throws Exception{
		System.out.println("지금 안되는 거야?");
		//session=request.getSession();
		MemberVO vo=(MemberVO)session.getAttribute("vo");
		System.out.println(vo+" "+"show");
		if(vo==null){			
			return new ModelAndView("home_main");
		}
		return new ModelAndView("show.mycinema","bvo",myCinemaService.show(request.getParameter("board_no")));
	}
	/**
	 * 수정 게시판으로 이동..
	 */
	public ModelAndView updateView(HttpServletRequest request,
			HttpServletResponse response,BoardVO bvo) throws Exception{
		System.out.println("이동합시다");
		System.out.println(bvo+" "+"updateview");
		return new ModelAndView("update.mycinema","bvo",myCinemaService.show(request.getParameter("board_no")));
	}
	public ModelAndView updateBoard(HttpServletRequest request,
			HttpServletResponse response,BoardVO bvo) throws Exception{
		System.out.println("게시판 내용 바꾸기");
		System.out.println(request.getParameter("board_no"));
		HttpSession session=request.getSession();
		MemberVO rvo=(MemberVO)session.getAttribute("vo");
		bvo.setMemberVO(rvo);
		System.out.println(bvo+"업데이트 보드");
		myCinemaService.updateBoard(bvo);
		System.out.println(bvo+"수정확인");
		return new ModelAndView("show.mycinema","bvo",myCinemaService.show(request.getParameter("board_no")));
	}
	public ModelAndView deleteBoard(HttpServletRequest request,
			HttpServletResponse response,BoardVO bvo) throws Exception{
		String no=request.getParameter("board_no");
		myCinemaService.deleteBoard(no);
		return new ModelAndView("cs_list.mycinema","lvo",myCinemaService.getBoardList("1"));
	}
	/**
	 * 답변글 게시판으로 이동
	 */
	public ModelAndView replyView(HttpServletRequest request,
			HttpServletResponse response,BoardVO bvo) throws Exception{
		String page=request.getParameter("pageNo");
		request.setAttribute("page", page);
		return new ModelAndView("reply.mycinema","bvo",myCinemaService.showNoCount(request.getParameter("board_no")));
	}
	/**
	 * 답변글 처리
	 */
	public ModelAndView reply(HttpServletRequest request, 
			 HttpServletResponse response,BoardVO bvo) throws Exception{
		HttpSession session=request.getSession();
		MemberVO rvo=(MemberVO)session.getAttribute("vo");
			bvo.setMemberVO(rvo);
			/*int ref=Integer.parseInt(request.getParameter("board_no"));
			bvo.setRef(ref);*/
			System.out.println("답변달겠습니다.");
			String isPublic = "0";
			bvo.setIsPublic(isPublic);
			bvo.getIsPublic();
			
			myCinemaService.reply(bvo);
			request.setAttribute("bvo", bvo);
			request.setAttribute("page", request.getParameter("page"));				
			return new ModelAndView("show.mycinema");
	}
	/**
	 * 회원정보수정
	 */
	public ModelAndView updateMember(HttpServletRequest request, 
			 HttpServletResponse response,MemberVO mvo) throws Exception{
		myCinemaService.updateMember(mvo);
		request.getSession().setAttribute("vo", mvo);
		return new ModelAndView("myInfo_result.mycinema");
	}
	/**
	 * 아이디 찾기 이동
	 */
	public ModelAndView findId(HttpServletRequest request, HttpServletResponse response) throws Exception{
		System.out.println("find");
		return new ModelAndView("findId.member");
	}
	/**
	 * 아이디 찾기
	 */
	public ModelAndView find(HttpServletRequest request, 
			 HttpServletResponse response,MemberVO mvo) throws Exception{		
		String id=request.getParameter("id");
		String day=request.getParameter("join_day");
		System.out.println(mvo.toString()+id+day);
		MemberVO rvo=myCinemaService.findId(mvo);
		if(rvo!=null){			
			System.out.println(rvo+"findId");
		}else{
			System.out.println("찾을 수 없음");
		}
		System.out.println("rvo : "+rvo); 
		return new ModelAndView("id_result.mycinema","vo",rvo);
	}
	/**
	 * 비밀번호 찾기 이동
	 */
	public ModelAndView findPass(HttpServletRequest request, 
					HttpServletResponse response,MemberVO mvo) throws Exception{
	
		return new ModelAndView("findPass.member");
	}
	/**
	 * 비밀번호 찾기
	 */
	public ModelAndView findPa(HttpServletRequest request, 
						HttpServletResponse response,MemberVO mvo) throws Exception{		
		System.out.println(mvo.toString());
		MemberVO rvo=myCinemaService.findPa(mvo);
		if(rvo!=null){
			System.out.println(rvo+"findId");
		}else{
			System.out.println("찾을 수 없음");
		}
		return new ModelAndView("pa_result.mycinema","vo",rvo);
	}
	/**
	 * 주민등록번호 비교
	 */
	public ModelAndView ssnCheck(HttpServletRequest request, 
						HttpServletResponse response) throws Exception{
		System.out.println("ssn 컨트롤러 시작");
		boolean ssnflag=false;
		try {
			ssnflag=myCinemaService.ssnCheck(request.getParameter("ssn"));		
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return new ModelAndView("JsonView","ssnflag",ssnflag);
	}	
	/**
	 * 영화검색
	 */
	public ModelAndView searchMovie(HttpServletRequest request, HttpServletResponse response){
		String search_title=request.getParameter("search_title");
		ArrayList<HashMap> list=null;
		try {
			list = myCinemaService.searchMovie(search_title);	
			Thread.sleep(1500);
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
		return new ModelAndView("JsonView","result",list);
	}
}
